package codeTop;

public class q33 {
    public int search(int[] nums, int target) {
        int l = 0;
        int r = nums.length-1;
        while (l < r) {
            int mid = (l+r)/2;
            if (nums[mid] == target) {
                return mid;
            }
            if (nums[mid] > target) {
                //连续区间在左边
                if (nums[l]<=nums[mid]){
                    if (target>=nums[l]){
                        r = mid-1;
                    }else {
                        l = mid+1;
                    }
                }else {
                    //连续区间在右边
                    r = mid-1;
                }
            }else {
                //连续区间在右边
                if (nums[r]>=nums[mid]){
                    if (target<=nums[r]){
                        l = mid+1;
                    }else {
                        r = mid-1;
                    }
                }else {
                    //连续区间在左边
                    l = mid+1;
                }
            }
        }
        return nums[l]==target?l:-1;
    }
    public static int binarySort(int[] nums ,int target){
        int l = 0;
        int r = nums.length-1;
        while (l < r) {
            int mid = (l+r)/2;
            if (nums[mid] > target) {
                r = mid-1;
            }else {
                l = mid+1;
            }
        }
        return l;
    }

    public static void main(String[] args) {
        System.out.println(binarySort(new int[]{1, 3, 4, 5, 6, 6, 8, 17}, 3));
    }
}
